package src.com.qf.service.impl;



import src.com.qf.dao.AddressDao;
import src.com.qf.dao.impl.AddressDaoImpl;
import src.com.qf.entity.Address;
import src.com.qf.service.AddressService;
import src.com.qf.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

public class AddressServiceImpl implements AddressService {
    private AddressDao dao = new AddressDaoImpl();
    @Override
    public List<Address> getAddressList(Integer uid) {
        List<Address> list = null;
        try {
            JdbcUtil.begin();

            list = dao.selectAddressByUid(uid);


            JdbcUtil.commit();
        } catch (SQLException e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int setDefault(Integer id, Integer uid) {
        int result = 0;
        try {
            JdbcUtil.begin();

           //将当前uid下的所有地址设为普通地址
            dao.updateLevelByUid(uid);

            //将id这个地址设为默认
            result = dao.updateLevelById(id);

            JdbcUtil.commit();
        } catch (SQLException e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public int addAddress(String strName, String strPhone, String strDetail, Integer uid) {
        int result = 0;
        try {
            JdbcUtil.begin();
            result = dao.insertAddress(strName,strPhone,strDetail,uid);
            JdbcUtil.commit();
        } catch (Exception e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int deleteAddress(String strId, Integer uid) {
        int result = 0;
        try {
            JdbcUtil.begin();
            result = dao.deleteAddressById(strId,uid);
            JdbcUtil.commit();
        } catch (Exception e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateAddress(String strName, String strPhone, String strDetail, Integer id, Integer uid) {
        int result = 0;
        try {
            result = dao.updateAddressById(strName,strPhone,strDetail,id,uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
